package com.atguigu.schedule.dao.impl;

import com.atguigu.schedule.POJO.SysUser;
import com.atguigu.schedule.dao.SysUesrDao;
import com.atguigu.schedule.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

public class SysUesrDaoImpl implements SysUesrDao {
    //获取连接池
    DataSource dataSource = JDBCUtil.getDataSource();

    @Override
    public int addUser(SysUser sysUser)  {

        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql = "insert into sys_user values(DEFAULT,?,?)";

        //插入一条数据
        try {
            int rows = queryRunner.update(sql, sysUser.getUsername(), sysUser.getUserPwd());
            return rows;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public SysUser findByUsername(SysUser sysUser) {
        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql = "select uid,username,user_pwd userPwd from sys_user where username=?";
        try {
            SysUser query = queryRunner.query(sql, new BeanHandler<SysUser>(SysUser.class), sysUser.getUsername());
            System.out.println("test:"+query);
            return query != null ?query:null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
